<template>
  <d2-container>
    <div class="mod-config">
      <div class="search">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" size="small">
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-input v-model="dataForm.deviceName" placeholder="计量器表名称" clearable></el-input>
          </el-form-item>
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-input v-model="dataForm.address" placeholder="计量器表地址" clearable></el-input>
          </el-form-item>
          <el-form-item style="margin-top:0 !important;margin-bottom:0 !important;">
            <el-button @click="getDataList()">查询</el-button>
            <el-button v-if="isAuth('ecs:ecsmeterinfo:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button v-if="isAuth('ecs:ecsmeterinfo:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;margin-top:15px;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="meterCode"
          header-align="center"
          align="center"
          label="计量器编号">
        </el-table-column>
        <el-table-column
          prop="meterName"
          header-align="center"
          align="center"
          label="计量器名称">
        </el-table-column>
        <el-table-column
          prop="meterAddr"
          header-align="center"
          align="center"
          label="计量器物理地址">
        </el-table-column>
        <el-table-column
          prop="meterModel"
          header-align="center"
          align="center"
          label="计量器型号">
        </el-table-column>
        <el-table-column
          prop="meterType"
          header-align="center"
          align="center"
          label="计量器类型">
          <template slot-scope="scope">
            {{scope.row.meterType |meterTypeName(meterTypeList)}}
          </template>
        </el-table-column>
  <!--      <el-table-column
          prop="meterStatus"
          header-align="center"
          align="center"
          label="计量器状态">
          <template slot-scope="scope">
            {{scope.row.meterStatus |statusName(statusList)}}
          </template>
        </el-table-column>

        <el-table-column
          prop="meterSetupaddr"
          header-align="center"
          align="center"
          label="计量器安装位置">
        </el-table-column>
        -->
        <el-table-column
          prop="equiCode"
          header-align="center"
          align="center"
          label="采集器">
          <template slot-scope="scope">
            {{scope.row.equiCode |colectName(collectDataList)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="sn"
          header-align="center"
          align="center"
          label="计量器序列号">
        </el-table-column>
        <el-table-column
          prop="meterLastupdate"
          header-align="center"
          align="center"
          label="计量器上次上报数据时间">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          width="180"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" size="small" @click="genQRCodeHandle(scope.row.meterCode)">生成二维码</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-4"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </d2-container>
</template>

<script>
  import AddOrUpdate from './ecsmeterinfo-add-or-update'
  import * as Util from "@/utils";
  export default {
    name: 'ecs_ecsmeterinfo',
    data () {
      return {
        dataForm: {
          deviceName: '',
          address: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        statusList:[{'label':'异常',"value":'0'},{'label':'正常',"value":'1'}],
        meterTypeList:[],
        collectDataList: [],
        meterModelList:[],
        potocolList:[]
      }
    },
    components: {
      AddOrUpdate
    },
    computed:{
      containerHeight() {
        return Util.getClientHeight() - 300;
      }
    },
    filters: {
      statusName: function (value,statusList) {
        for(let i=0;i<statusList.length;i++){
          if(statusList[i].value===value){
            return statusList[i].label;
          }
        }
      },
      meterTypeName: function (value,statusList) {
        for(let i=0;i<statusList.length;i++){
          if(statusList[i].paramKey===value){
            return statusList[i].paramValue;
          }
        }
      },
      colectName: function (value,statusList) {
        for(let i=0;i<statusList.length;i++){
          if(statusList[i].equiCode===value){
            return statusList[i].equiName+'['+statusList[i].equiCode+']';
          }
        }
      }
    },
    activated () {
      this.queryStatic("METER_TYPE")
      this.getCollectDataList()
      this.getDataList()
      this.getMeterModelList()
    },
    methods: {
      getCollectDataList () {
        this.$api.okhttp({
          url: '/ecs/ecscollectequi/list',
          method: 'get',
          params: {
            'page': 1,
            'limit': 10000000,
            "customerId": this.$util.cookies.get('customer_id'),
          }
        }).then((data) => {
          if (data && data.code === 0) {
          this.collectDataList = data.page.list
        } else {
          this.collectDataList = []
        }
      })
      },
      getMeterModelList(){
        this.$api.okhttp({
          url: '/ecs/ecsmetermodel/potocolList',
          method: 'get',
          params: {
            'page': 1,
            'limit': 10000000,
            "customerId": this.$util.cookies.get('customer_id')
          }
        }).then((data) => {
          if (data && data.code === 0) {
          this.meterModelList = data.list
          this.potocolList = data.potocolList
        } else {
          this.meterModelList = []
        }
      })
      },
      /**
       * 获取参数枚举
       */
      queryStatic(type) {
        this.$api.okhttp({
          url: '/sys/sysstaticdata/getList',
          method: 'get',
          params: {
            'code': type ,
            "customerId":this.$util.cookies.get('customer_id')
          }
        }).then((data) => {
          if (data && data.code === 0) {
          this.meterTypeList = data.page
        } else {
          this.meterTypeList = []
        }
      });
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$api.okhttp({
          url: '/ecs/ecsmeterinfo/list',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'deviceName': this.dataForm.deviceName,
            'address': this.dataForm.address,
            "customerId":this.$util.cookies.get('customer_id'),
            'projectid': this.$util.cookies.get("cuuren_projectId"),
            'deviceType':'0' //只编辑正常表
          }
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,this.meterTypeList ,this.collectDataList,this.meterModelList,this.potocolList)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.okhttp({
            url: '/ecs/ecsmeterinfo/delete',
            method: 'post',
            data: ids,
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //生成二维码
      genQRCodeHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[${ids.join(',')}]生成二维码操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.okhttp({
            url: '/ecs/ecsmeterinfo/genQRCode',
            method: 'post',
            data: ids,
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
